package xcmg.device.web.setting;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.setting.PrintTemplateService;
import xcmg.device.service.vo.setting.PrintTemplateVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;

import java.util.List;

/**
 * Created by sqq on 2018/2/5.
 */
@RequestMapping("/printTemplate")
@RestController
public class PrintTemplateController extends BasicController {
    @Autowired
    private PrintTemplateService printTemplateService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(PrintTemplateVO printTemplateVO) throws Exception {
        printTemplateVO.setOrgId(getOrgId());
        List<PrintTemplateVO> results=printTemplateService.list(printTemplateVO);
        return actionValueResult(results);
    }

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.PrintTpl.ADD)
    public ActionResult insert(@RequestBody PrintTemplateVO printTemplateVO) throws Exception {
        printTemplateVO.setCreateId(getUserId());
        printTemplateVO.setOrgId(getOrgId());
        ErrorCode code=printTemplateService.insert(printTemplateVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.PrintTpl.EDIT)
    public ActionResult update (@RequestBody PrintTemplateVO printTemplateVO){
        ErrorCode code=printTemplateService.update(printTemplateVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/setDefault",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.PrintTpl.DEFAULT)
    public ActionResult setDefault(@RequestBody PrintTemplateVO printTemplateVO){
        ErrorCode code=printTemplateService.setDefault(printTemplateVO.getId());
        return actionResult(code);
    }
    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.PrintTpl.DEL)
    public ActionResult remove(@RequestBody PrintTemplateVO printTemplateVO){
        ErrorCode code=printTemplateService.remove(printTemplateVO.getId());
        return actionResult(code);
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        PrintTemplateVO printTemplateVO=printTemplateService.item(id);
        return actionValueResult(printTemplateVO);
    }

    @RequestMapping(value = "/defaultItem",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult defaultItem(PrintTemplateVO printTemplateVO) throws Exception {
        if(printTemplateVO==null){
            return actionResult(ErrorCode.IllegalArument);
        }
        if(printTemplateVO.getId()==null){
            printTemplateVO.setOrgId(getOrgId());
        }
        PrintTemplateVO result=printTemplateService.defaultItem(printTemplateVO.getOrgId(),printTemplateVO.getTemplateType());
        return actionValueResult(result);
    }

}
